import nyNetwork from "@/api/nyBaseNetwork";
import DZModelPage from "@/model/DZModelPage";
import nyMessageToast from "@/utils/NYMessageToast";
import DZModelGoods from "../model/DZModelGoods";

class DZServiceGoods {


    m_page = new DZModelPage(10);

    m_screen_list = { type: null, name: '' }

    list_goods: Array<DZModelGoods> = [];

    m_goods = new DZModelGoods();

    list_type_data = [];

    list_assess_dic = [];

    list_export_dic = [];

    list_goods_dic = [];


    net_list_goods() {
        var arr_data = [];
        nyNetwork.net("商品列表", { ...this.m_screen_list, ...this.m_page }, data => {
            this.m_page.total = data.total;
            for (const item of data.list) {
                var model = new DZModelGoods();
                model.id = item.iid ?? '';
                model.listCover = item.ilistCover ? nyNetwork.image(item.ilistCover) : '';
                model.name = item.sname ?? '';
                model.type = item.itype ?? 1;
                model.typeStr = item.itype == 1 ? '测评' : '咨询';
                model.truePrice = item.itruePrice / 100;
                model.activity = item.iactivity == 1
                model.sort = item.isort ?? 0
                model.top = item.itop ?? 0
                arr_data.push(model);
            }
            this.list_goods = arr_data;
        })
    }

    net_list_goods_dic() {
        var arr_data = [];
        nyNetwork.net("商品字典列表", {}, data => {
            for (const item of data) {
                arr_data.push({
                    id: item.iid,
                    name: item.sname
                });
            }
            this.list_goods_dic = arr_data;
        })
    }

    net_del_goods(id, success: () => void) {
        nyNetwork.net("删除商品", { id: id }, data => {
            success();
        })
    }

    net_save_sort(id,sort, success: () => void) {
        nyNetwork.net("保存商品排序", { id: id ,sort:sort}, data => {
            success();
        })
    }

    net_move_up(id, success: () => void) {
        nyNetwork.net("商品上移", { id: id }, data => {
            success();
        })
    }
    net_move_down(id, success: () => void) {
        nyNetwork.net("商品下移", { id: id }, data => {
            success();
        })
    }
    net_move_top(id, success: () => void) {
        nyNetwork.net("商品置顶", { id: id }, data => {
            success();
        })
    }
    net_cancel_top(id, success: () => void) {
        nyNetwork.net("商品取消置顶", { id: id }, data => {
            success();
        })
    }

    net_list_assess_dic() {
        var arr_data = [];
        nyNetwork.net("测评字典列表", {}, data => {
            for (const item of data) {
                arr_data.push({
                    id: item.iId ?? '',
                    name: item.sName ?? ''
                });
            }
            this.list_assess_dic = arr_data;
            this.list_type_data = arr_data;
        })
    }

    net_list_export_dic() {
        var arr_data = [];
        nyNetwork.net("专家字典列表", {}, data => {
            for (const item of data) {
                arr_data.push({
                    id: item.iId ?? '',
                    name: item.sName ?? ''
                });
            }
            this.list_export_dic = arr_data;
        })
    }

    net_add_goods(success: () => void) {
        if (this.m_goods.name.nyIsEmpty()) {
            nyMessageToast.error("请输入商品名称");
            return;
        }
        if (!this.m_goods.listCover) {
            nyMessageToast.error("请上传列表封面");
            return;
        }
        if (!this.m_goods.infoCover) {
            nyMessageToast.error("请上传详情封面");
            return;
        }
        if (!this.m_goods.reportCover && this.m_goods.type == 1) {
            nyMessageToast.error("请上传报告封面");
            return;
        }
        if (!this.m_goods.typeId) {
            nyMessageToast.error("请选择关联内容");
            return;
        }
        if (this.m_goods.truePrice <= 0) {
            nyMessageToast.error("请输入商品现价");
            return;
        }
        if (this.m_goods.desc.nyIsEmpty()) {
            nyMessageToast.error("请输入商品详情");
            return;
        }
        if (this.m_goods.notice.nyIsEmpty()) {
            nyMessageToast.error("请输入购买须知");
            return;
        }
        nyNetwork.net("添加商品", {
            ...this.m_goods,
            activity: this.m_goods.activity ? 1 : 0
        }, data => {
            success();
        })
    }

    net_info_goods(id) {
        nyNetwork.net("商品详情", { id: id }, data => {
            this.m_goods.parse(data);
        })
    }

    net_edit_goods(success: () => void) {
        if (this.m_goods.name.nyIsEmpty()) {
            nyMessageToast.error("请输入商品名称");
            return;
        }
        if (!this.m_goods.listCover) {
            nyMessageToast.error("请上传列表封面");
            return;
        }
        if (!this.m_goods.infoCover) {
            nyMessageToast.error("请上传详情封面");
            return;
        }
        if (!this.m_goods.reportCover && this.m_goods.type == 1) {
            nyMessageToast.error("请上传报告封面");
            return;
        }
        if (!this.m_goods.typeId) {
            nyMessageToast.error("请选择关联内容");
            return;
        }
        if (this.m_goods.truePrice <= 0) {
            nyMessageToast.error("请输入商品现价");
            return;
        }
        if (this.m_goods.desc.nyIsEmpty()) {
            nyMessageToast.error("请输入商品详情");
            return;
        }
        if (this.m_goods.notice.nyIsEmpty()) {
            nyMessageToast.error("请输入购买须知");
            return;
        }
        console.log("修改商品", this.m_goods)
        nyNetwork.net("修改商品", { ...this.m_goods, activity: this.m_goods.activity ? 1 : 0 }, data => {
            success();
        })
    }

}
export default DZServiceGoods;